package com.test.travelinformation.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.test.travelinformation.R;
import com.test.travelinformation.activity.CollectedActivity;
import com.test.travelinformation.activity.ReleaseActivity;
import com.test.travelinformation.adapter.CircleAdapter;
import com.test.travelinformation.dao.TravelDao;
import com.test.travelinformation.entity.Collect;
import com.test.travelinformation.entity.Travel;
import com.test.travelinformation.utils.SPUtils;

import java.util.ArrayList;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class Homefragment extends Fragment {
    RecyclerView recyclerView;
    CircleAdapter adapte;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment, container, false);
        recyclerView = view.findViewById(R.id.recyclerView);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initview();
        TravelDao travelDao = new TravelDao();
        travelDao.getAllData(adapte);
    }
    private void initview() {
        adapte = new CircleAdapter(getActivity(), new ArrayList<>());
        adapte.setOnItemLongClickListener((adapter, view, position) -> {
            new QMUIDialog.MessageDialogBuilder(getContext())
                    .setTitle("收藏信息")
                    .setMessage("确认要收藏这条信息吗？")
                    .addAction("取消", (dialog, index) -> dialog.dismiss())
                    .addAction(0, "确定", QMUIDialogAction.ACTION_PROP_NEGATIVE, (dialog, index) -> {
                        Travel travel = ((Travel)adapter.getData().get(position));
                        Collect collect = new Collect();
                        collect.setTravelId(travel.getObjectId());
                        collect.setUserId(SPUtils.get(getContext(),"userId","").toString());
                        collect.setUserName(SPUtils.get(getContext(),"username","").toString());
                        collect.save(new SaveListener<String>() {
                            @Override
                            public void done(String s, BmobException e) {
                                if (e == null){
                                    Toast.makeText(getContext(),"收藏成功",Toast.LENGTH_SHORT).show();
                                }else {
                                    Toast.makeText(getContext(),"收藏失败"+e.toString(),Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        dialog.dismiss();
                    }).create().show();
            return false;
        });
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        recyclerView.setAdapter(adapte);
    }
}
